import { useModel } from "umi";
import { useReactive } from "ahooks";
import { useEffect } from "react";

const useAllGateways = (personUuid) => {
    const { remoteData } = useModel("global");

    const state = useReactive({
        loading: false,
        gateways: [],
    });

    const loadPersonGateways = async () => {
        if (!personUuid) return;
        state.loading = true;
        let res = await remoteData({
            object: "base-platform-management-service",
            serviceName: "/person/gateway/getAll",
            params: {
                personUuid,
            },
        });
        state.gateways = res || [];
        state.loading = false;
    };

    useEffect(() => {
        personUuid && loadPersonGateways();
    }, [personUuid]);

    return { ...state, loadPersonGateways };
};

export default useAllGateways;
